package apigateway.project.core.helper;

import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Request;
import org.asynchttpclient.Response;

import java.util.concurrent.CompletableFuture;

/**
 * 异步http辅助类,辅助NettyHttpClient
 */
public class AsyncHttpHelper {
    private static final class SingletonHolder{
        private static final AsyncHttpHelper INSTANCE = new AsyncHttpHelper();
    }
    private AsyncHttpHelper(){}

    public static AsyncHttpHelper getInstance(){
        return SingletonHolder.INSTANCE;
    }

    private AsyncHttpClient asyncHttpClient;

    public void initilize(AsyncHttpClient asyncHttpClient){
        this.asyncHttpClient = asyncHttpClient;
    }

    public CompletableFuture<Response> executeRequest (Request request){
        ListenableFuture<Response> future = asyncHttpClient.executeRequest(request);
        return future.toCompletableFuture();
    }

}
